<template>
  <div>
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="48" :sm="48">
            <span class="table-page-search-submitButtons" :style="{ float: 'left', overflow: 'hidden' }">
              <a-button type="primary" @click="handleAdd()">新增</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table ref="table" size="default" rowKey="key" :columns="columns" :data="loadData" :rowSelection="rowSelection"
      showPagination="auto">
      <span slot="serial" slot-scope="text, record, index">
        <a @click="handleRead(record)">{{ record.name }}</a>
      </span>

      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleEdit(record)">修改</a>
          <a-divider type="vertical" />
          <a @click="handleDel(record)">删除</a>
        </template>
      </span>
    </s-table>

    <specialModal ref="specialModal" :visible="visible" :loading="confirmLoading" :model="mdl" @cancel="handleCancel"
      @ok="handleOk"
      :mdlStatus="mdlStatus" />
  </div>
</template>
<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'
import { getClientsList, updateClients, delClients, addClients } from '@/api/custom'
import { getSpecialList, addSpecial, delSpecial, updateSpecial } from '@/api/special'
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import specialModal from './modules/specialModal'
import { createVNode, defineComponent } from 'vue';
import { Modal } from 'ant-design-vue';

const columns = [
  {
    title: '序列号',
    dataIndex: 'serialNumber',
  },
  {
    title: '平台',
    dataIndex: 'tagName'
  },
  {
    title: '产物类型',
    dataIndex: 'internalStorage',
  },
  {
    title: '版本',
    dataIndex: 'versionNumber',
  },
  {
    title: '备注',
    dataIndex: 'msg',
  },
  {
    title: '创建时间',
    dataIndex: 'creationTime',
    sorter: true
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '150px',
    scopedSlots: { customRender: 'action' }
  }
]

const statusMap = {
  0: {
    status: 'default',
    text: '关闭'
  },
  1: {
    status: 'processing',
    text: '运行中'
  },
  2: {
    status: 'success',
    text: '已上线'
  },
  3: {
    status: 'error',
    text: '异常'
  }
}

export default {
  name: 'TableList',
  components: {
    STable,
    Ellipsis,
    specialModal
  },
  data() {
    this.columns = columns
    return {
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        return getSpecialList(requestParameters)
          .then(res => {
            return res.data
          })
      },
      selectedRowKeys: [],
      selectedRows: [],
      mdlStatus:'',
    }
  },
  filters: {
    statusFilter(type) {
      return statusMap[type].text
    },
    statusTypeFilter(type) {
      return statusMap[type].status
    }
  },
  created() {
  },
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    handleEdit(record) {
      this.mdlStatus = 'edit'
      this.mdl = { ...record }
      this.visible = true
    },
    handleAdd() {
      this.mdlStatus = 'add'
      this.visible = true
    },
    handleOk(versionList){
      if(this.mdlStatus == 'add'){
        this.handleAddOk()
      } else{
        this.handleEditOk(versionList)
      }
    },
    handleEditOk(versionList) {
      const form = this.$refs.specialModal.form
      this.confirmLoading = true
      let _self = this
      form.validateFields((errors, values) => {
        if (!errors) {
          console.log('%c [  ]-194', 'font-size:13px; background:pink; color:#bf2c9f;', values)
          values.versionsId = values.versionNumber
          if(versionList){
            try {
              for (let i = 0; i < versionList.length; i++) {
                if(values.versionNumber == versionList[i].label ) {
                  values.versionsId = versionList[i].value
                  break;
                }
              }
            } catch (error) {
              
            }
          }
          updateSpecial(values).then((res) => {
            if (res.data.status == 200) {
              _self.$message.success(`修改成功`)
              _self.visible = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              _self.$refs.table.refresh()
            } else {
              _self.$message.error(res.data.msg)
            }
            _self.confirmLoading = false
          })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleAddOk() {
      const form = this.$refs.specialModal.form
      this.confirmLoading = true
      let _self = this
      form.validateFields((errors, values) => {
        if (!errors) {
          console.log('%c [  ]-194', 'font-size:13px; background:pink; color:#bf2c9f;', values)
          values.versionsId = values.versionNumber
          addSpecial(values).then((res) => {
            if (res.data.status == 200) {
              _self.$message.success(`新增成功`)
              _self.visible = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              _self.$refs.table.refresh()
            } else {
              _self.$message.error(res.data.msg)
            }
            _self.confirmLoading = false
          })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleOk2() {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          console.log('values', values)
          if (values.id > 0) {
            // 修改 e.g.
            new Promise((resolve, reject) => {
              setTimeout(() => {
                resolve()
              }, 1000)
            }).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.success('修改成功')
            })
          } else {
            // 新增
            new Promise((resolve, reject) => {
              setTimeout(() => {
                resolve()
              }, 1000)
            }).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.success('新增成功')
            })
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.visible = false
      // 清理表单数据
      const form = this.$refs.specialModal.form
      form.resetFields()
    },
    handleDel(record) {
      let _self = this
      Modal.confirm({
        title: `确定删除序列号${record.serialNumber}吗`,
        // icon: createVNode(ExclamationCircleOutlined),
        // content: createVNode('div', { style: 'color:red;' }, 'Some descriptions'),
        onOk() {
          console.log('OK');
          delSpecial({ id: record.id }).then(() => {
            _self.$message.success(`删除成功`)
            // 刷新表格
            _self.$refs.table.refresh()
            _self.confirmLoading = false
          })
        },
        onCancel() {
          console.log('Cancel');
        },
        class: 'test',
      });

      // if (record.status !== 0) {
      //   this.$message.info(`${record.no} 订阅成功`)
      // } else {
      //   this.$message.error(`${record.no} 订阅失败，规则已关闭`)
      // }
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    resetSearchForm() {
      this.queryParam = {
        "name": "",
        "page": 0,
        "pageNum": 1,
        "pageSize": 10,
        "tagSystem": ""
      }
    }
  }
}
</script>